ARG IMAGE

FROM $IMAGE

ENV RUNUSER=nginx

ARG NGINX_VERSION
ARG ZLIB_VERSION
ARG PCRE_VERSION
ARG OPENSSL_VERSION

ENV NGINX_VERSION=$NGINX_VERSION

RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirror.nju.edu.cn|g' \
    -i /etc/yum.repos.d/CentOS-*.repo \
 && yum install -y epel-release \
 && sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!//download\.fedoraproject\.org/pub!//mirror.nju.edu.cn!g' \
    -e 's!//download\.example/pub!//mirror.nju.edu.cn!g' \
    -e 's!http://mirrors!https://mirrors!g' \
    -i /etc/yum.repos.d/epel*.repo

RUN yum -y install --setopt=tsflags=nodocs gcc-c++ make rpm-build rpmdevtools wget

ARG BASE_CONFIGURE_ARGS="\
	--prefix=%{_sysconfdir}/nginx \
	--sbin-path=%{_sbindir}/nginx \
	--modules-path=%{_libdir}/nginx/modules \
	--conf-path=%{_sysconfdir}/nginx/nginx.conf \
	--error-log-path=%{_localstatedir}/log/nginx/error.log \
	--http-log-path=%{_localstatedir}/log/nginx/access.log \
	--pid-path=%{_localstatedir}/run/nginx.pid \
	--lock-path=%{_localstatedir}/run/nginx.lock \
	--http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \
	--http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \
	--http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \
	--http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \
	--http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \
        --with-pcre=../pcre-${PCRE_VERSION} \
	--with-zlib=../zlib-${ZLIB_VERSION} \
	--with-openssl=../openssl-${OPENSSL_VERSION} \
	--user=%{nginx_user} \
	--group=%{nginx_group} \
	--with-http_gzip_static_module \
	--with-http_gunzip_module \
	--with-http_ssl_module \
	--with-http_v2_module \
	--with-stream"

ARG BASE_RELEASE=1 \
    PACKAGE_VENDOR='NGINX Packaging <nginx-packaging@f5.com>'
    
ADD sources /sources
ADD nginx.spec.in /nginx.spec.in

RUN rpmdev-setuptree \
 && wget --no-check-certificate http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz -O ~/rpmbuild/SOURCES/nginx-${NGINX_VERSION}.tar.gz \
 && wget --no-check-certificate http://www.zlib.net/zlib-${ZLIB_VERSION}.tar.gz -O ~/rpmbuild/SOURCES/zlib-${ZLIB_VERSION}.tar.gz \
 && wget --no-check-certificate https://nchc.dl.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz -O ~/rpmbuild/SOURCES/pcre-${PCRE_VERSION}.tar.gz \
 && wget --no-check-certificate https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz -O ~/rpmbuild/SOURCES/openssl-${OPENSSL_VERSION}.tar.gz

RUN cp /sources/* ~/rpmbuild/SOURCES/ \
 && cat /nginx.spec.in | sed \
    -e "s#%%PACKAGE_VENDOR%%#${PACKAGE_VENDOR}#g" \
    -e "s#%%BASE_VERSION%%#${NGINX_VERSION}#g" \
    -e "s#%%BASE_RELEASE%%#${BASE_RELEASE}#g" \
    -e "s#%%BASE_CONFIGURE_ARGS%%#${BASE_CONFIGURE_ARGS}#g" \
    -e "s#%%ZLIB_VERSION%%#${ZLIB_VERSION}#g" \
    -e "s#%%PRCE_VERSION%%#${PCRE_VERSION}#g" \
    -e "s#%%OPENSSL_VERSION%%#${OPENSSL_VERSION}#g" \
    > ~/rpmbuild/SPECS/nginx.spec \
 && cat  ~/rpmbuild/SPECS/nginx.spec

WORKDIR /root/rpmbuild/RPMS/x86_64
